 Ext.namespace("admin.lecture");

admin.lecture.lecture = function(panelId, title, iconClz){
    var lectureStore = new Ext.data.JsonStore({
        url : "admin/lecture/findByCon",
        fields : ["id", "title", "summary", "speaker", "startTime", "duration", "location",
            "wxNewsUrl", "snapshoot", "status", "createDate", "updateDate"],
        totalProperty : "total",
        root : "data",
        autoLoad : true,
        listeners : {
            beforeload : function(s){
                s.removeAll();
                Ext.apply(s.baseParams, {
                    keyword : queryField.getValue(),
                    status : statusCombo.getValue()
                });
            }
        }
    });
    var lectureSm = new Ext.grid.RowSelectionModel({
        singleSelect : true
    });
    var lectureCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header : "讲座标题",
        dataIndex : "title",
        width : 300
    }, {
        header : "主讲人",
        dataIndex : "speaker"
    }, {
        header : "地点",
        dataIndex : "location",
        width : 280
    }, {
        header : "活动时间",
        dataIndex : "startTime",
        renderer : renderTime2Minutes,
        width : 150
    }, {
        header : "状态",
        dataIndex : "status",
        renderer : renderEnumStatus
    }, {
        header : "创建日期",
        dataIndex : "createDate",
        renderer : renderDate
    }]);
    var statusCombo = genStatusCombo();
    var queryField = genQueryField();
    var queryBar = ["->", "状态：", statusCombo, "关键词：",  queryField, {
        text : "查询",
        iconCls : "query",
        handler : function(){
            lectureStore.load();
        }
    }];
    var lectureGrid = new Ext.grid.GridPanel({
        store : lectureStore,
        cm : lectureCm,
        sm : lectureSm,
        frame : true
    });
    return new Ext.Panel({
        id : panelId,
        title : title,
        iconCls : iconClz,
        closable : true,
        layout : "hbox",
        layoutConfig : {
            align : "stretch"
        },
        items : [lectureGrid],
        tbar : genToolBar(genOptBarByFn(add, del, update, toggleStatus), queryBar, panelId),
        bbar : genPageBar(lectureStore)
    });

    function add() {
        var win = genAddorUpdateWin("admin/lecture/add");
        win.show();
    }
    function del(){
        var rec = lectureSm.getSelected();
        if(rec==undefined){
            showFailMsg("请选择讲座！");
            return;
        }
        confirmDelete(remove);
        function remove(){
            ajaxRequest({
                url : "admin/lecture/delete/" + rec.get("id"),
                callback : function(){
                    lectureStore.reload();
                }
            });
        }
    }
    function update(){
        var rec = lectureSm.getSelected();
        if(rec==undefined){
            showFailMsg("请选择讲座！");
            return;
        }
        var win = genAddorUpdateWin("admin/lecture/update");
        win.findByType("form")[0].getForm().loadRecord(rec);
        win.show();
    }
    function toggleStatus(){
        var rec = lectureSm.getSelected();
        if(rec==null){
            showFailMsg("请选择讲座！");
            return;
        }
        ajaxRequest({
            url : "admin/lecture/toggleStatus/" + rec.get("id"),
            callback : function(){
                lectureStore.reload();
            }
        });
    }
    function genAddorUpdateWin(url){
        return new Ext.Window({
            title : "新增或修改讲座",
            layout : "fit",
            modal : true,
            iconCls : "save",
            height : 330,
            width : 690,
            bodyStyle : 'padding:5px;',
            items : genForm(url)
        });
        function genForm(url){
            return new Ext.form.FormPanel({
                url : url,
                labelWidth : 60,
                labelAlign : "right",
                baseCls: 'x-plain',
                layout : "hbox",
                items : [{
                    flex : 1,
                    layout: "form",
                    frame : true,
                    items : [{
                        xtype : "hidden",
                        name : "id",
                        //value设置-1，为空则controller检验失败
                        value : "-1"
                    }, {
                        xtype : "textfield",
                        fieldLabel : "讲座名称",
                        allowBlank : false,
                        name : "title"
                    }, {
                        xtype : "textfield",
                        fieldLabel : "主讲人",
                        allowBlank : false,
                        name : "speaker"
                    }, {
                        fieldLabel : "开始时间",
                        allowBlank : false,
                        name : "startTime",
                        xtype : "datetimefield"
                    }, {
                        fieldLabel : "持续时间",
                        allowBlank : false,
                        name : "duration",
                        xtype : "numberfield",
                        emptyText : "单位（分钟）"
                    }, {
                        xtype : "textfield",
                        fieldLabel : "讲座地点",
                        allowBlank : false,
                        name : "location"
                    }, {
                        xtype : "textfield",
                        fieldLabel : "新闻链接",
                        allowBlank : false,
                        name : "wxNewsUrl"
                    }, genEnumStatusCombo()
                ]}, {
                    flex : 1,
                    layout: "form",
                    frame : true,
                    items : [{
                        xtype: 'box',
                        width: 320, //图片宽度
                        height: 200, //图片高度
                        id : "event_snapshoot_view",
                        autoEl: {
                            tag: 'img',     //指定为img标签
                            src: ''         //指定url路径
                        }
                    }, {
                        xtype : "textfield",
                        hidden : true,
                        name : "snapshoot"
                    }],
                    buttons : [new Ext.Button({
                        text : "选择微信预览图",
                        handler : uploadSnapshoot
                    })],
                    buttonAlign : "center"
                }],
                buttonAlign : "center",
                buttons : [{
                    text : "保存",
                    iconCls : "save",
                    handler : function(b){
                        submitForm(b.findParentByType("form"), refreshEventGrid);
                        function refreshEventGrid(){
                            lectureStore.reload();
                        }
                    }
                }, {
                    text : "重置",
                    iconCls : "reset",
                    type : "reset",
                    handler : function(b){
                        b.findParentByType("form").getForm().reset();
                    }
                }]
            });
            function uploadSnapshoot() {
                var form = new Ext.form.FormPanel({
                    fileUpload : true,
                    frame : true,
                    labelWidth: 30,
                    url : "admin/event/snapshoot",
                    defaults : {
                        anchor : "100%",
                        allowBlank : false
                    },
                    items : [{
                        fieldLabel : "文件",
                        emptyText: '请选择文件',
                        anchor: '100%',
                        xtype : "fileuploadfield",
                        name : 'snapshoot',
                        buttonText : "浏览"
                    }],
                    buttons : [{
                        text : "确定",
                        iconCls : "save",
                        handler : function(b){
                            submitForm(b.findParentByType("form"), refreshSnapshoot);
                            function refreshSnapshoot(json){
                                var snapshootPath = json.result.data;
                                // 更新讲座快照
                                var view = Ext.getCmp("event_snapshoot_view");
                                view.getEl().dom.src = "resources/temp/" + snapshootPath;
                                // 更新照片
                                view.nextSibling().setValue(snapshootPath);
                            }
                        }
                    }, {
                        text : "重置",
                        iconCls : "reset",
                        handler : function(b){
                            b.findParentByType("form").getForm().reset();
                        }
                    }]
                });
                var win = new Ext.Window({
                    title : "上传文件",
                    width : 300,
                    height : 150,
                    layout : "fit",
                    frame : true,
                    modal : true,
                    items : [form]
                });
                win.show();
            }
        }
    }

}

registerModule("admin.lecture.lecture", admin.lecture.lecture);
